package com.agile.leetcode.anotation;

import com.agile.leetcode.enumdemo.ShopCode;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author ChenZhangKun
 * @Date 2020/11/23 15:28
 */
public class Person {
    @MyTest
    public void add() {
        System.out.println("add...");
    }

    public void test() throws InvocationTargetException, IllegalAccessException {
        System.out.println(ShopCode.SHOP_ORDER_FAIL.getMessage());
        // 拿到类对象
        Class<Person> personClass = Person.class;
        // 拿到其中的方法
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            // 是否被自定义注解标记
            if (declaredMethod.isAnnotationPresent(MyTest.class)) {
                // 反射执行
                declaredMethod.invoke(new Person());
            }
        }
    }

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        new Person().test();
    }
}
